package com.example.homework7;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class bindService extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public bindService() {
        super(R.layout.bindservice);
    }
    private MyConn myConn;
    public class MyConn implements ServiceConnection{
        private MusicService2.MyBinder binder = null;
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (MusicService2.MyBinder)service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().startService(new Intent(getContext(), MusicService2.class));
        if(myConn == null){
            myConn = new MyConn();
            getActivity().bindService(
                    new Intent(getActivity(),MusicService2.class),myConn,0);
        }
        getView().findViewById(R.id.button10).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(myConn.binder == null)
                            return;
                        myConn.binder.play();
                    }
                }
        );
        getView().findViewById(R.id.button11).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(myConn.binder == null)
                            return;
                        myConn.binder.next();
                    }
                }
        );
        getView().findViewById(R.id.button12).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(myConn.binder == null)
                            return;
                        myConn.binder.pause();
                    }
                }
        );
    }

    // TODO: Rename and change types and number of parameters
    public static bindService newInstance(String param1, String param2) {
        bindService fragment = new bindService();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.bindservice, container, false);
    }
}